package com.example.demo.repository;

import com.example.demo.entity.Inventory;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
 * 库存数据访问层
 */
@Repository
public interface InventoryRepository extends JpaRepository<Inventory, Long> {
    
    Optional<Inventory> findByProductIdAndWarehouseId(Long productId, Long warehouseId);
    
    List<Inventory> findByProductId(Long productId);
    
    List<Inventory> findByWarehouseId(Long warehouseId);
    
    List<Inventory> findByStatus(Inventory.InventoryStatus status);
    
    @Query("SELECT i FROM Inventory i WHERE i.currentStock <= i.minStockLevel")
    List<Inventory> findLowStockItems();
    
    @Query("SELECT i FROM Inventory i WHERE i.currentStock = 0")
    List<Inventory> findOutOfStockItems();
    
    @Query("SELECT i FROM Inventory i WHERE i.currentStock > i.maxStockLevel")
    List<Inventory> findOverstockItems();
}
